package com.qst.service.ssm;

import com.qst.ExamException;
import com.qst.action.question.QuestionQueryParam;
import com.qst.entity.Choice;
import com.qst.entity.PersonalityDimension;
import com.qst.entity.Question;
import com.qst.mapper.ChoiceMapper;
import com.qst.mapper.QuestionMapper;
import com.qst.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceSsmImpl implements QuestionService {

    private final QuestionMapper questionMapper;
    private final ChoiceMapper choiceMapper;

    @Autowired
    public QuestionServiceSsmImpl(QuestionMapper questionMapper, ChoiceMapper choiceMapper) {
        this.questionMapper = questionMapper;
        this.choiceMapper = choiceMapper;
    }

    @Override
    public List<Question> find(QuestionQueryParam param) {
        List<Question> questions;
        if (param.getAssessmentId() > 0) {
            //执行根据条件查询操作
            questions = questionMapper.findByParam(param);
        } else {
            //查询全部
            questions = questionMapper.findAll();
        }
        return questions;
    }

    @Override
    public Question findById(int id) {
        return questionMapper.findById(id);
    }

    @Override
    public List<Choice> findChoices(Integer id) {
        return choiceMapper.findByQuestion(id);
    }

    @Override
    public List<PersonalityDimension> findDimensionByQuestion(Integer id) {
        return questionMapper.findDimensionByQuestion(id);
    }

    @Override
    public void save(Question q, List<Choice> choices) {
        isChoiceValid(q.getType(), choices);
        q.setStatus(2);
        questionMapper.insert(q);
        for (Choice ch : choices) {
            ch.setQuestionId(q.getId());
            choiceMapper.insert(ch);
        }
    }

    @Override
    public List<Integer> findDimensionIdByQuestion(int qid) {
        return questionMapper.findDimension(qid);
    }

    @Override
    public void attachDimension(int qid, int[] pids) {
        questionMapper.detachDimension(qid);
        for (int p : pids) {
            questionMapper.attachDimension(qid, p);
        }
    }

    @Override
    public void delete(int questionId) {
        if (questionMapper.isUsed(questionId)) {
            throw new ExamException("使用中的题目，不能被删除");
        }
        questionMapper.detachDimension(questionId);
        choiceMapper.deleteByQuestion(questionId);
        questionMapper.delete(questionId);
    }

    @Override
    public void update(Question q, List<Choice> choices) {
        isChoiceValid(q.getType(), choices);
        questionMapper.update(q);
        for (Choice ch : choices) {
            ch.setQuestionId(q.getId());
            choiceMapper.update(ch);
        }
    }

    private void isChoiceValid(int type, List<Choice> choices) {
        int count = 0;
        for (Choice ch : choices) {
            if (ch.isChecked()) {
                count++;
            }
        }
        if (count == 0) {
            throw new ExamException("请选择本题的正确选项");
        }
        if (type == 1 && count != 1) {
            throw new ExamException("单选题只能有一个正确选项");
        }
        if (type == 2 && count < 2) {
            throw new ExamException("多选题至少要有两个正确选项");
        }
    }
}
